﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNETPerformance.App_BusinessLogic.BasePages;
using ASPNETPerformance.App_BusinessLogic.Helpers;
using ASPNETPerformance.App_BusinessLogic.JsAndCss;
using ASPNETPerformance.App_BusinessLogic.Utils;

namespace ASPNETPerformance.UserControls {
    public partial class Part2 : BaseUserControl {
        protected void Page_Load( object sender, EventArgs e ) {
            if ( !this.Page.IsPostBack ) {
                BindPart2();
            }
        }

        private void BindPart2() {
            this.cbWebZip.Checked = CommonHelper.GetCookieBool( "IsWebZip" );

            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtDefaultByJs", true ) ) ) {
                CommonHelper.SetCookie( "txtDefaultByJs",
                   "Public.all.js", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtDefaultInputsByJs", true ) ) ) {
                CommonHelper.SetCookie( "txtDefaultInputsByJs",
                   @"_assets\js\jquery-1.4.4.min.js,_assets\js\public.js", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtDefaultByCss", true ) ) ) {
                CommonHelper.SetCookie( "txtDefaultByCss",
                    "Public.all.css", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtDefaultInputsByCss", true ) ) ) {
                CommonHelper.SetCookie( "txtDefaultInputsByCss",
                    @"_assets\css\core.css,_assets\css\grid.css,_assets\css\round.css,_assets\css\sinorcaish-screen.css,_assets\css\CustomStyles.css", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }

            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtCombineNameByJs", true ) ) ) {
                CommonHelper.SetCookie( "txtCombineNameByJs",
                   "Combine.all.js", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtCombineInputsByJs", true ) ) ) {
                CommonHelper.SetCookie( "txtCombineInputsByJs",
                  @"_assets\js\jquery-1.4.4.min.js,_assets\js\public.js", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtCombineNameByCss", true ) ) ) {
                CommonHelper.SetCookie( "txtCombineNameByCss",
                    "Combine.all.css", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }
            if ( string.IsNullOrEmpty( CommonHelper.GetCookieString( "txtCombineInputsByCss", true ) ) ) {
                CommonHelper.SetCookie( "txtCombineInputsByCss",
                    @"_assets\css\CustomStyles.css,_assets\css\sinorcaish-screen.css", new TimeSpan( 365, 0, 0, 0, 0 ) );
            }

            this.txtDefaultByJs.Text = CommonHelper.GetCookieString( "txtDefaultByJs", true );
            this.txtDefaultInputsByJs.Text = CommonHelper.GetCookieString( "txtDefaultInputsByJs", true );
            this.txtDefaultByCss.Text = CommonHelper.GetCookieString( "txtDefaultByCss", true );
            this.txtDefaultInputsByCss.Text = CommonHelper.GetCookieString( "txtDefaultInputsByCss", true );
            this.cbEnableDefaultJsAndCss.Checked = CommonHelper.GetCookieBool( "IsEnableDefaultJsAndCss" );

            this.txtCombineNameByJs.Text = CommonHelper.GetCookieString( "txtCombineNameByJs", true );
            this.txtCombineInputsByJs.Text = CommonHelper.GetCookieString( "txtCombineInputsByJs", true );
            this.txtCombineNameByCss.Text = CommonHelper.GetCookieString( "txtCombineNameByCss", true );
            this.txtCombineInputsByCss.Text = CommonHelper.GetCookieString( "txtCombineInputsByCss", true );
            this.cbEnableCombine.Checked = CommonHelper.GetCookieBool( "IsCombine" );

            this.cbEnableStaticCacheByJsAndCss.Checked = CommonHelper.GetCookieBool( "IsStaticCacheByJsAndCss" );
        }

        protected void BtnPart2GzipCompressionJsAndCssFiles_Click( object sender, EventArgs e ) {
            JsAndCssManager.Execute( Server.MapPath( "~/_assets" ) );
            this.lblPart2Result.Text = "保存成功。";
        }

        protected void BtnPart2_Click( object sender, EventArgs e ) {
            SaveInfo();
            this.lblPart2Result.Text = "保存成功。";
        }

        public void SaveInfo() {
            CommonHelper.SetCookie( "IsWebZip",
           this.cbWebZip.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "txtDefaultByJs",
                this.txtDefaultByJs.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "txtDefaultInputsByJs",
                this.txtDefaultInputsByJs.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "txtDefaultByCss",
                this.txtDefaultByCss.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "txtDefaultInputsByCss",
                this.txtDefaultInputsByCss.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            if ( this.cbDefaultJsAndCssNewCreate.Checked ) {
                JsAndCssManager.ExecuteCombineByJs( Server.MapPath( "~/" ),
                                           this.txtDefaultInputsByJs.Text.Split( new[] { ',' },
                                                                                StringSplitOptions.RemoveEmptyEntries ),
                                           this.txtDefaultByJs.Text );

                JsAndCssManager.ExecuteCombineByCss( Server.MapPath( "~/" ),
                                                this.txtDefaultInputsByCss.Text.Split( new[] { ',' },
                                                                                     StringSplitOptions.RemoveEmptyEntries ),
                                                this.txtDefaultByCss.Text );
            }

            CommonHelper.SetCookie( "IsEnableDefaultJsAndCss",
                this.cbEnableDefaultJsAndCss.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "txtCombineNameByJs",
               this.txtCombineNameByJs.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "txtCombineInputsByJs",
                this.txtCombineInputsByJs.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "txtCombineNameByCss",
                this.txtCombineNameByCss.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );
            CommonHelper.SetCookie( "txtCombineInputsByCss",
                this.txtCombineInputsByCss.Text.Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            if ( this.cbCombineNewCreate.Checked ) {
                JsAndCssManager.ExecuteCombineByJs( Server.MapPath( "~/" ),
                                                 this.txtCombineInputsByJs.Text.Split( new[] { ',' },
                                                                                      StringSplitOptions.RemoveEmptyEntries ),
                                                 this.txtCombineNameByJs.Text );

                JsAndCssManager.ExecuteCombineByCss( Server.MapPath( "~/" ),
                                                this.txtCombineInputsByCss.Text.Split( new[] { ',' },
                                                                                     StringSplitOptions.RemoveEmptyEntries ),
                                                this.txtCombineNameByCss.Text );
            }

            // Note bug
            JsAndCssManager.Execute( Server.MapPath( "~/_assets" ) );

            CommonHelper.SetCookie( "IsCombine",
               this.cbEnableCombine.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

            CommonHelper.SetCookie( "IsStaticCacheByJsAndCss",
               this.cbEnableStaticCacheByJsAndCss.Checked.ToString().Trim(), new TimeSpan( 365, 0, 0, 0, 0 ) );

        }
    }
}